package com.navinfo.opentsp.platform.computing.util;

import com.google.protobuf.InvalidProtocolBufferException;
import com.googlecode.protobuf.format.JsonFormat;
import com.navinfo.opentsp.platform.location.protocol.common.RealTimeDataPb;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间转换类
 */
public class DateUtils {

    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static String format(long mills, String pattern){
       return DateFormatThreadLocal.getFormat(pattern).format(mills);
    }

    public static String format(long mills){
        return DateFormatThreadLocal.getFormat().format(mills);
    }

    public static String format(Date date){
        return DateFormatThreadLocal.getFormat().format(date);
    }

    public static String format(Date date, String pattern){
        return DateFormatThreadLocal.getFormat(pattern).format(date);
    }

    private static class DateFormatThreadLocal {
        private static ThreadLocal<SimpleDateFormat> THREAD_SAFE_FORMATTER = new ThreadLocal<SimpleDateFormat>(){
            @Override
            protected synchronized SimpleDateFormat initialValue() {
                SimpleDateFormat sd = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
                sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                return sd;
            }
        };

        public static SimpleDateFormat getFormat(){
            return THREAD_SAFE_FORMATTER.get();
        }

        public static SimpleDateFormat getFormat(String pattern){
            SimpleDateFormat format = getFormat();
            format.applyPattern(pattern);
            return format;
        }
    }

    public static void main(String[] args) {
        byte[] aa = {8, -77, -31, -111, -38, 5, 16, -67, -32, -78, 17, 24, -116, -24, -15, 60, 32, -99, 54, 40, -114, 61, 48, -96, 13, 56, -22, 51, 64, -82, 58, 72, -99, 52, 80, -35, 71, 88, -19, 28, 96, -10, 49, 104, -4, 30, 112, -29, 20, 120, -119, 65, -128, 1, -69, 29, -120, 1, -6, 54, -112, 1, -78, 38, -104, 1, -38, 29, -96, 1, -17, 76, -88, 1, -109, 42, -80, 1, -34, 22, -72, 1, -121, 7, -64, 1, -17, 4, -56, 1, -11, 44, -48, 1, -128, 44, -40, 1, -116, 48, -32, 1, -78, 11, -24, 1, -37, 36, -16, 1, -38, 61, -8, 1, -68, 13, -128, 2, 1, -128, 2, 10, -120, 2, -4, 47, -112, 2, -67, 44, -104, 2, -117, 51, -96, 2, -82, 22, -88, 2, -64, 54, -80, 2, -89, 36, -72, 2, -44, 27, -112, 3, -128, -42, -124, -28, 51, -32, 3, -88, 49, -24, 3, -52, 51, -16, 3, -121, 25, -8, 3, -45, 46, -128, 4, -10, 16, -120, 4, -22, 64, -112, 4, -108, 59, -104, 4, -33, 69, -96, 4, -13, 67, -88, 4, -24, 16, -80, 4, -96, 21, -72, 4, -52, 63, -64, 4, -96, 12, -56, 4, 8, -48, 4, -16, 37, -40, 4, -60, 14, -32, 4, -97, 69, -24, 4, -3, 50, -16, 4, -40, 44, -8, 4, -92, 27, -64, 2, -66, 73, -56, 2, -81, 38, -48, 2, -69, 61, -40, 2, -117, 39, -32, 2, -86, 40, -24, 2, -116, 67, -16, 2, -102, 51, -8, 2, -103, 19, -128, 3, -17, 29, -120, 3, -26, 44, -104, 3, -78, 47, -96, 3, -100, 14, -88, 3, -34, 13, -80, 3, -24, 8, -72, 3, -14, 58, -64, 3, -91, 48, -56, 3, -88, 69, -48, 3, -22, 18, -40, 3, -47, 53};
        try {
            RealTimeDataPb.RealTimeData aaa = RealTimeDataPb.RealTimeData.parseFrom(aa);


        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }
}
